<?php


namespace Frieza\Modules\RiskContent\Controllers;

use App\Http\Controllers\Controller;
use Doraemon\cache\Queue;
use Doraemon\model\AdmBestUser;
use Doraemon\model\AdmOperateLog;
use Doraemon\model\AdmRecommendUser;
use Doraemon\model\Member;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Doraemon\service\Themis\Log as ThemisLog;
use Doraemon\model\Themis\Log as ThemisLogModel;

class BatchForbiddenController extends Controller
{
    public function batchForbiddenInfo(Request $request)
    {
        $code = 1;
        try {
            $uids = $request->input('ids', []);
            $adminId = Admin::user()->id;
            $adminName = Admin::user()->username;
            if (!empty($uids)) {
                foreach ($uids as $uid) {
                    //删除用户在推荐表的数据
                    AdmRecommendUser::deleteByUid($uid);
                    //删除精华表数据
                    AdmBestUser::deleteByUid($uid);
                    //将用户的状态设置为冻结
                    $member_data['status'] = Member::STATUS_BLOCKED;
                    $res = Member::updateUserInfo($uid,$member_data);
                    // 添加themis日志
                    $aOpt = array(
                        'admin_id'    => $adminId,
                        'remark'      => "该用户账号已被封禁(批量)",
                        'status'      => ThemisLogModel::STATUS_BLOCKED,
                        'action_type' => ThemisLogModel::ACTION_TYPE_ADD_BLOCK,
                        'illegal_or_punish' => ThemisLogModel::ACTION_PUNISH,
                    );
                    $aThemisLog = ThemisLog::fetchThemisLogData($uid, $aOpt);
                    ThemisLog::add($aThemisLog);
                    if ($res){
                        Queue::insertQueue('User/force_out_user', array(
                            'uid' => $uid,
                        ), Queue::QUEUE_COMMON);
                        Queue::insertQueue('User/deleteSearch', array(
                            'uid' => $uid,
                        ), Queue::QUEUE_COMMON);
                        Queue::insertQueue('User/delTokenCache', array(
                            'uid' => $uid,
                        ), Queue::QUEUE_COMMON);
                        Queue::insertQueue('User/delUserinfoCache', array(
                            'uid' => $uid,
                        ), Queue::QUEUE_COMMON);
                        Queue::insertQueue('DataUserProfile/delete', array(
                            'uid' => $uid,
                        ), Queue::QUEUE_COMMON);
                        AdmOperateLog::addLog($uid,AdmOperateLog::$operate_type['ban_user'],$adminId,$adminName);
                        $msg = '批量封禁成功';
                    } else {
                        $msg = '批量封禁失败，请稍后重试';
                    }
                }
            }
        } catch (\Exception $e) {
            $code = 0;
            $msg = $e->getMessage();
        }
        return compact('code', 'msg');
    }

}